package com.itcast.contacts;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText mEtName,mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd,mBtnQuery,mBtnUpdate,mBtnDelete;
    private SQLiteDatabase db;
    private MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper =new MyHelper(this);
        db=myHelper.getWritableDatabase();
        init();
    }

    private void query(){
        Cursor cursor=db.query("information",null,null,null,null,null,null);
        if (cursor.getCount()==0){
            Toast.makeText(this,"没有数据", Toast.LENGTH_SHORT).show();
        }else{
            while (cursor.moveToNext()){
                mTvShow.append("\n"+"Name:"+cursor.getString(1)+";Tel :"+cursor.getString(2));
            }
        }
        cursor.close();

    }
    private void init(){
        mEtName=findViewById(R.id.mEtName);
        mEtPhone=findViewById(R.id.mEtPhone);
        mTvShow=findViewById(R.id.mTvShow);
        mBtnAdd=findViewById(R.id.mBtnAdd);
        mBtnQuery=findViewById(R.id.mBtnQuery);
        mBtnUpdate=findViewById(R.id.mBtnUpdate);
        mBtnDelete=findViewById(R.id.mBtnDelete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }
    public void onClick(View v){
        String name,phone;
        ContentValues values;
        switch(v.getId()){
            case R.id.mBtnAdd:
                name=mEtName.getText().toString();
                phone=mEtPhone.getText().toString();
                values=new ContentValues();
                values.put ("name",name);
                values.put("phone",phone);
                long id=db.insert("information",null,values);
                if (id!=-1){
                    Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
                    query();
                }else{
                    Toast.makeText(this,"信息添加失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mBtnQuery:
                query();
                break;
            case R.id.mBtnUpdate:
                Cursor cursor1=db.query("information",null,null,null,null,null,null);
                if (cursor1.getCount()==0){
                    Toast.makeText(this,"没有数据可修改",Toast.LENGTH_SHORT).show();

                }else{
                    values=new ContentValues();
                    values.put("phone",phone=mEtPhone.getText().toString());
                    int rows=db.update("information",values,"name=?",
                            new String[]{mEtName.getText().toString()});
                    if(rows>0){
                        Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
                        query();
                    }else{
                        Toast.makeText(this,"信息修改失败",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.mBtnDelete:
                Cursor cursor2=db.query("information",null,null,null,null,null,null);
                if(cursor2.getCount()==0){
                    Toast.makeText(this,"没有数据可删除",Toast.LENGTH_SHORT).show();
                }else{
                    int delRows=db.delete("information",null,null);
                    if(delRows>0){
                        Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
                        mTvShow.setText("");
                    }else{
                        Toast.makeText(this,"信息删除失败",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }
}
 class MyHelper extends SQLiteOpenHelper{
    public MyHelper(Context context){
        super(context,"itcast.db",null,1);
    }
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
    }

     @Override
     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

     }
 }